///////////////////////////////////////////////////////////
///////////////// Have an itch? Scratch it! ///////////////
///////////////////////// SCRATCH /////////////////////////
/////////////////////  A MUD  Server   ////////////////////
///////////////////// By: Jared Devall ////////////////////
/////////////////////      Thanks:     ////////////////////
/////////////////////  DIKU/Merc/ROM   ////////////////////
///////////////////// Aetas/Deus Gang  ////////////////////
/////////////////////       Beej       ////////////////////
///////////////////////////////////////////////////////////

#include "definitions.h"
#include <list>
#include "commands.h"
#include "commandTable.h"
#include "character.h"
#include "world.h"
#include "stringutil.h"
#include "split.h"
#include "timestamp.h"

using namespace std;

// pCommand definitions
// (Keep in alphabetical order... it's MUCH easier)
pCmdAge::pCmdAge() {
	SetName( "age" );
	SetLevel( GOD );
	SetEnabled( true );	
	SetOmniset( false );
}

bool pCmdAge::Execute( Character* c, const string& args ) {
	std::string buf;

	if ( args.empty() ) {
		c->Send( "syntax: age <age>\n\r" );
		return false;
	}

	c->getCharacter()->setAge( atoi( args.c_str() ) );

	buf << c->getCharacter()->getName() << "'s age set to " << c->getCharacter()->getAge() << "\n\r";
	c->Send( buf );

	buf = "Your age has been changed to ";
	buf << c->getCharacter()->getAge() << "\n\r";
	c->getCharacter()->Send( buf );

	return true;
}

pCmdCommands::pCmdCommands() {
	SetName( "commands" );
	SetLevel( 1 );
	SetEnabled( true );	
	SetOmniset( false );
}

bool pCmdCommands::Execute( Character* c, const string& args ) {
	std::string buf;
	std::string temp;
	list< Command* >::iterator it;
	int f = 0;

	if ( c->getLevel() >= HEAD ) {
		buf << "\n\r\t{chead (level " << HEAD << ") commands:{x\n\r";
		for ( it = pCommandTable::Instance()._commands.begin(); it != pCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == HEAD ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	if ( f != 0 && f != 5 )
		buf << "\n\r";
	f = 0;
	if ( c->getLevel() >= GOD ) {
		buf << "\n\r\t{cgod (level " << GOD << ") commands:{x\n\r";
		for ( it = pCommandTable::Instance()._commands.begin(); it != pCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == GOD ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	if ( f != 0 && f != 5 )
		buf << "\n\r";
	f = 0;
	if ( c->getLevel() >= BUILDER ) {
		buf << "\n\r\t{cbuilder (level " << BUILDER << ") commands:{x\n\r";
		for ( it = pCommandTable::Instance()._commands.begin(); it != pCommandTable::Instance()._commands.end(); ++it ) {
			if ( (*it)->_level == BUILDER ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
			    f++;
			    if ( f == 5 ) {
					buf << "\n\r";
					f = 0;
				}
			}
		}
	}
	if ( f != 0 && f != 5 )
		buf << "\n\r";
	f = 0;
	buf << "\n\r\t{c-- Commands --{x \n\r";
	for ( it = pCommandTable::Instance()._commands.begin(); it != pCommandTable::Instance()._commands.end(); ++it ) {
		if ( (*it)->_level == 1 ) {
				if ( !(*it)->_enabled )
					buf << "{w";
				if ( f == 0 ) {
					buf << "  " << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				} else {
					buf << (*it)->_name.c_str() << spaces( 12, (*it)->_name.c_str(), temp );
				}
				if ( !(*it)->_enabled )
					buf << "{x";
		    f++;
		    if ( f == 5 ) {
				buf << "\n\r";
				f = 0;
			}
		}
	}

	if ( f != 0 && f != 5 )
		buf << "\n\r";

	c->Send( buf );

	return true;
}
pCmdExit::pCmdExit() {
	SetName( "exit" );	
	SetLevel( 1 );
	SetEnabled( true );
	SetOmniset( true );
}

bool pCmdExit::Execute( Character* c, const string& args ) {

	c->setCharacter( 0 );
	c->SetStatus( CONNECTED );
	c->Send( "You have exited pedit mode.\n\r" );

	return true;
}

pCmdEchoColor::pCmdEchoColor() {
	SetName( "gcolor" );
	SetLevel( GOD );
	SetEnabled( true );
	SetOmniset( false );
}

bool pCmdEchoColor::Execute( Character* c, const string& args ) {
	std::string buf;

	if ( args.empty() ) {
		c->Send( "syntax: lname <name>\n\r" );
		return false;
	}
	if ( c->getCharacter()->getLevel() < GOD ) {
		buf = c->getCharacter()->getName();
		buf << " doesn't have gecho.\n\r";
		c->Send( buf );
		return false;
	}

	char color = args[0];

	c->getCharacter()->SetEchocolor( color );
	buf << "Your gecho color has been changed to " << c->getCharacter()->GetEchocolor() << " ({" << c->getCharacter()->GetEchocolor() << "color{x)\n\r";
	c->getCharacter()->Send( buf );

	buf = c->getCharacter()->getName();
	buf << "'s gecho color changed to " << c->getCharacter()->GetEchocolor() << " ({" << c->getCharacter()->GetEchocolor() << "color{x)\n\r";
	c->Send( buf );

	return true;
}

pCmdGender::pCmdGender() {
	SetName( "gender" );
	SetLevel( GOD );
	SetEnabled( true );	
	SetOmniset( false );
}

bool pCmdGender::Execute( Character* c, const string& args ) {
	std::string buf;
	int gender = 0;

	if ( args.empty() ) {
		c->Send( "syntax: gender <male | female>\n\r" );
		return false;
	}

	if ( args == "m" || args == "male" )
		gender = MALE;
	if ( args == "f" || args == "female" )
		gender = FEMALE;
	if ( args == "n" || args == "neutral" )
		gender = NEUTRAL;

	c->getCharacter()->setGender( gender );

	buf << c->getCharacter()->getName() << "'s gender set to " << c->getCharacter()->stringGender() << "\n\r";
	c->Send( buf );

	buf = "Your gender has been changed to ";
	buf << c->getCharacter()->stringGender() << "\n\r";
	c->getCharacter()->Send( buf );

	return true;
}

pCmdLname::pCmdLname() {
	SetName( "lname" );
	SetLevel( GOD );
	SetEnabled( true );	
	SetOmniset( false );
}

bool pCmdLname::Execute( Character* c, const string& args ) {
	std::string buf;
	std::string name = args;

	if ( args.empty() ) {
		c->Send( "syntax: lname <name>\n\r" );
		return false;
	}

	c->getCharacter()->SetLname( name.c_str() );
	buf << "Your last name has been changed to " << c->getCharacter()->GetLname() << "\n\r";
	c->getCharacter()->Send( buf );

	buf = c->getCharacter()->getName();
	buf << "'s last name changed to " << c->getCharacter()->GetLname() << "\n\r";
	c->Send( buf );

	return true;
}

pCmdName::pCmdName() {
	SetName( "name" );
	SetLevel( GOD );
	SetEnabled( true );	
	SetOmniset( false );
}

bool pCmdName::Execute( Character* c, const string& args ) {
	std::string buf;
	std::string name = args;

	if ( args.empty() ) {
		c->Send( "syntax: name <name>\n\r" );
		return false;
	}

	c->getCharacter()->setName( name );
	buf << "Your name has been changed to " << c->getCharacter()->getName() << "\n\r";
	c->getCharacter()->Send( buf );

	buf = c->getCharacter()->getName();
	buf << "'s name changed to " << c->getCharacter()->getName() << "\n\r";
	c->Send( buf );

	return true;
}
